package com.feihong.txgw.ui.activity.kaika;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.ui.activity.base.BaseActivity;
import com.feihong.txgw.ui.activity.mendian.FuJinMenDianActivity;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import org.json.JSONObject;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.ui.activity.kaika
 * @class describe:  开卡主界面   选择持卡开户和无卡开户
 * @author wangziheng
 * @time 2017/9/18 17:54
 * @change
 * @chang time
 * @class describe
 */
public class KaikaHomeActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.kaika_home_middle_divider)
    View mMiddleDivider;
    @BindView(R.id.kaika_home_card_btn)
    ImageView mHaveCardBtn;
    @BindView(R.id.kaika_home_yuyue_btn)
    ImageView mNoCardBtn;
    @BindView(R.id.kaika_home_ziti_btn)
    ImageView mSelfCardBtn;
    @BindView(R.id.kaika_home_chika_layout)
    RelativeLayout mChiKaLayout;
    @BindView(R.id.kaika_home_chika_note_tv)
    TextView mChikaNoteTitle;
    @BindView(R.id.kaika_home_chika_note)
    TextView mChikaNote;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadLayout;
    @BindView(R.id.kaika_home_layout)
    RelativeLayout mKaikaHomeLayout;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectErrorLayout;
    @BindView(R.id.re_query_data_layout)
    LinearLayout mRequeryLayout;
    @BindView(R.id.top_back_layout)
    RelativeLayout mTitleLayout;
    private Disposable mDisposable;

    @Override
    public void setTheme() {
    }

    @Override
    public int setLayoutID() {
        return R.layout.kaika_home_layout;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setPageTitle("手机开卡");
        setBackButtonFinish();
        KaikaManager.getmKaikaManager().addStack(this);
        mRequeryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mConnectErrorLayout.setVisibility(View.GONE);
                mLoadLayout.setVisibility(View.VISIBLE);
                queryKaikaType();
            }
        });
    }

    @Override
    public void bindData() {
        mHaveCardBtn.setOnClickListener(this);
        mNoCardBtn.setOnClickListener(this);
        mSelfCardBtn.setOnClickListener(this);
        mLoadLayout.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        mKaikaHomeLayout.setVisibility(View.VISIBLE);

        queryKaikaType();

    }

    private void queryKaikaType() {

        String cityCode = UserLoginInfo.getUserCityInfo().getCityCode();
        if(TextUtils.isEmpty(cityCode)){
            return;
        }
        RetrofitManager.getInstance().getKaikaType(cityCode).compose(RxHelper.<String>rxSchedulerHelper()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                if(mDisposable != null && !mDisposable.isDisposed()){
                    mDisposable.dispose();
                }
                mDisposable = d;
            }

            @Override
            public void onNext(String result) {
                try {
                    if(activityIsDestory()){
                        return;
                    }
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject typeObj = jsonObject.optJSONObject("data");
                    String type = typeObj.optString("show");
                    if(!"1".equals(type)){
                        mChiKaLayout.setVisibility(View.VISIBLE);
                        mChikaNote.setVisibility(View.VISIBLE);
                        mChikaNoteTitle.setVisibility(View.VISIBLE);
                        mMiddleDivider.setVisibility(View.VISIBLE);
                    }
                    mConnectErrorLayout.setVisibility(View.GONE);
                    mLoadLayout.setVisibility(View.GONE);
                    mKaikaHomeLayout.setVisibility(View.VISIBLE);
                }catch (Exception err){
                    mConnectErrorLayout.setVisibility(View.VISIBLE);
                    mLoadLayout.setVisibility(View.GONE);
                    mKaikaHomeLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            //预约开卡
            case R.id.kaika_home_yuyue_btn:
                intent = new Intent(this,KaikaNoCardOperateActivity.class);
                startActivity(intent);
                break;
            //持卡开户
            case R.id.kaika_home_card_btn:
                intent = new Intent(this,KaiKaActivity.class);
                startActivity(intent);
                break;
            //用户自提------》附近门店
            case R.id.kaika_home_ziti_btn:
                intent = new Intent(this,FuJinMenDianActivity.class);
                intent.putExtra("ziti","zt");
                startActivity(intent);
                break;
            default:break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mDisposable != null){
            mDisposable.dispose();
        }
    }
}
